using UnityEngine;
using UnityEngine.AI;

public class NPCStop : MonoBehaviour
{
    private NavMeshAgent navMeshAgent;
    public float interactionRange = 3f;

    void Start()
    {
        navMeshAgent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        // Check if the player is in range
        if (IsPlayerInRange())
        {
            // Stop NPC movement
            if (!navMeshAgent.isStopped)
            {
                StopMovement();
            }
        }
        else
        {
            if (navMeshAgent.isStopped)
            {
                ResumeMovement();
            }
                // Resume NPC movement
        }
    }

    void StopMovement()
    {
        // Stop the NavMeshAgent
        navMeshAgent.isStopped = true;
        //navMeshAgent.acceleration = 0;
        //navMeshAgent.speed = 0;
    }

    void ResumeMovement()
    {
        // Resume the NavMeshAgent if it was stopped
        if (navMeshAgent.isStopped)
        {
            navMeshAgent.isStopped = false;
        }
    }

    bool IsPlayerInRange()
    {
        // Assuming the player is tagged as "Player". You can adjust this based on your player setup.
        GameObject player = GameObject.FindGameObjectWithTag("Player");

        if (player != null)
        {
            float distance = Vector3.Distance(player.transform.position, transform.position);
            return distance <= interactionRange;
        }

        return false;
    }
}
